package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Role;
import com.lin.services.AuthorityService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Class:AuthorityController
 * Package:com.lin.controller
 *
 * @author hellcat
 * Date: 2019/12/20
 * ♣♣♣   权限管理
 */

@Controller
@RequestMapping("/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @MyLog("查看角色")
    @RequiresPermissions("stuff:authority:list")
    @RequestMapping(value = "/toAuthority", method = RequestMethod.GET)
    public String toAuthority(HttpServletRequest request) {
        request.setAttribute("ddId", "Authority");
        return "authorityMain";
    }

    @RequiresPermissions("stuff:authority:list")
    @RequestMapping(value = "/getRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getRoleInfo(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        List<Role> list = authorityService.getRoleInfo();
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("stuff:authority:list")
    @RequestMapping(value = "/getAuthorityByRoleName", method = RequestMethod.GET)
    public String getAuthorityByRoleName(Role role, HttpServletRequest request) {
        Map<String, Object> map = authorityService.getAuthorityByRoleId(role);
        request.setAttribute("authority", map.get("data"));
        request.setAttribute("role", role);
        return "authorityEdit";
    }

    @RequiresPermissions("stuff:authority:update")
    @RequestMapping(value = "updateRoleAuthority", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateRoleAuthority(HttpServletRequest request) {
        String checkedBox = request.getParameter("checkedBox");
        String roleId = request.getParameter("roleId");
        String roleName = request.getParameter("roleName");
        return authorityService.updateRoleAuthority(roleId, checkedBox,roleName);
    }

//    @RequiresPermissions("authority:deleteRole")
    @RequestMapping(value = "deleteRole", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteRole(HttpServletRequest request) {
        String roleId = request.getParameter("id");
        return authorityService.deleteRoleById(roleId);
    }

    @RequiresPermissions("stuff:authority:insert")
    @RequestMapping(value = "insertRole", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertRole(HttpServletRequest request) {
        String roleName = request.getParameter("roleName");
        String authority = request.getParameter("authority");
        return authorityService.insertRole(roleName, authority);
    }


}
